/**
 * @FileName: LoginController.java
 * @Author
 * @Description:
 * @Date 2016年1月4日 上午11:39:53
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.client;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.model.Banner;
import cn.hpclub.server.model.MemberOrg;
import cn.hpclub.server.restful.API;
import cn.hpclub.server.service.TokenService;
import cn.hpclub.server.util.SystemConfigUtil;

@API("/ProductServer/Artivities")
public class ActivitiesController extends BaseClientController<MemberOrg>{
    private static Logger       log                = LoggerFactory.getLogger(DzgController.class);
    private static final String VP_SERVER_BASE_URL = "http://192.168.1.120:8000/HPClubServer";
    public static String        hostPrefix         = SystemConfigUtil.getSystemConfig().getHostPrefix();

    /**
     * @Title: GainAdvertisementList
     * @Description: 获得广告列表信息（首页banner） void
     */
    @Before(Tx.class)
    public void GainAdvertisementList(){
        BannerResult bannerResut = new BannerResult();
        try{
            int orgId = TokenService.service.getOrgIdFromToken(getPara(ClientApiConstant.Key.TOKEN));
            int serverType = getParaToInt("serverType",1);
            List<Banner> list = Banner.dao.getBannerList(orgId,serverType);
            for(Banner banner : list){
                BannerBean bean = getBannerBean(banner);
                bannerResut.addToList(bean);
            }
            bannerResut.resultCode = 1000;
            bannerResut.message = "获取广告列表成功";
        }
        catch(Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            bannerResut.resultCode = 1004;
            bannerResut.message = "服务器处理异常";
            bannerResut.clearList();
        }
        // log.info("banner:" + JSON.toJSONString(bannerResut, true));
        renderJson(JSON.toJSONString(bannerResut, true));
    }

    /**
     * @Title: getBannerBean
     * @Description:TODO
     * @param banner
     * @return BannerBean
     */
    private BannerBean getBannerBean(Banner banner){
        BannerBean bean = new BannerBean();
        bean.id = String.valueOf(banner.getInt("id"));
        bean.activityName = banner.getStr("name");
        bean.activityImage = getActivityImageUrl(banner);
        bean.thumbnails = bean.activityImage;
        bean.activityUrl = banner.getStr("activity_url");
        bean.startTime = "";
        bean.endTime = "";
        String addTime = "";
        if(banner.getDate("created_time") != null){
            DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            addTime = format.format(banner.getDate("created_time"));
        }
        bean.addTime = addTime;
        bean.description = "";
        bean.secondType = String.valueOf(banner.getInt("type"));
        bean.activityID = bean.id;
        bean.productID = "0";
        return bean;
    }

    /**
     * @Title: getActivityImageUrl
     * @Description:TODO
     * @param banner
     * @return String
     */

    private String getActivityImageUrl(Banner banner){
        String coverImageUrl = banner.getStr("cover_image");
        // log.info("coverImageUrl=" + coverImageUrl);
        if(StrKit.isBlank(coverImageUrl)){
            coverImageUrl = "";
        } else{
            if(!coverImageUrl.startsWith("http://")){
                coverImageUrl = getHostPrefix() + coverImageUrl;
            }
        }
        return coverImageUrl;
    }

    private String getHostPrefix(){
        return StrKit.isBlank(hostPrefix) ? VP_SERVER_BASE_URL : hostPrefix;
    }

    public static class BannerResult{
        @JSONField(name = "ResultCode", ordinal = 1)
        public int              resultCode;
        @JSONField(name = "Message", ordinal = 2)
        public String           message;
        @JSONField(name = "Data", ordinal = 3)
        public List<BannerBean> list = new LinkedList<BannerBean>();

        public void addToList(BannerBean bean){
            list.add(bean);
        }

        public void clearList(){
            list.clear();
        }
    }

    public static class BannerBean{
        @JSONField(ordinal = 1)
        public String id;           // 活动id
        @JSONField(ordinal = 2)
        public String activityName; // 活动名称
        @JSONField(ordinal = 3)
        public String activityImage;// 活动图片
        @JSONField(ordinal = 4)
        public String coverImage;   // 封面图片
        @JSONField(ordinal = 5)
        public String thumbnails;   // 缩略图片
        @JSONField(ordinal = 7)
        public String activityUrl;  // 活动url
        @JSONField(ordinal = 8)
        public String startTime;    // 开始时间
        @JSONField(ordinal = 9)
        public String endTime;      // 结束时间
        @JSONField(ordinal = 10)
        public String addTime;      // 添加日间
        @JSONField(ordinal = 11)
        public String description;  // 描述
        @JSONField(ordinal = 12)
        public String secondType;   // 0:外链 1：专题 2:文本
        @JSONField(ordinal = 13)
        public String activityID;   // 与活动id一致
        @JSONField(ordinal = 14)
        public String productID;    // secondType=1,则此处代表活动id,点击进入活动详细页面(类似点击爆款),secondType=3
                                    // 表示单个商品（产品id），点击进入商品详细页面
    }
}
